package com.hsav.kcxb.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hsav.kcxb.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

public class UnisonListActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unison_list);
        initView();
    }

    private void initView() {
        ListView unisonListView = (ListView)findViewById(R.id.listview_unison);
        UnisonAdapter unisonAdapter = new UnisonAdapter(this);
        unisonListView.setAdapter(unisonAdapter);

        View textToSing = findViewById(R.id.text_to_sing);
        textToSing.setOnClickListener(this);
        View textToScore = findViewById(R.id.text_to_score);
        textToScore.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.text_to_sing:
                startActivity(new Intent(this, RecordActivity.class));
                break;
            case R.id.text_to_score:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("给伴奏打分");

                View ratingBarView = LayoutInflater.from(this).inflate(R.layout.layout_score, null);
                final RatingBar ratingBar = (RatingBar) ratingBarView.findViewById(R.id.rating_bar);
                builder.setView(ratingBarView);
                builder.setPositiveButton("打分", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(UnisonListActivity.this, String.format("score: %s", ratingBar.getRating()), Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
                Button btnPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                btnPositive.setTextColor(getResources().getColor(R.color.colorAccent));
                Button btnNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                btnNegative.setTextColor(getResources().getColor(R.color.color_gray));
                break;
        }
    }

    private class UnisonAdapter extends BaseAdapter implements View.OnClickListener {

        private final Context mContext;
        private final LayoutInflater mInfalter;

        public UnisonAdapter(Context context){
            this.mContext = context;
            this.mInfalter = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return 16;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if(convertView==null){
                convertView = mInfalter.inflate(R.layout.item_unison,parent,false);
                viewHolder = new ViewHolder();
                viewHolder.mHeadImg = (ImageView) convertView.findViewById(R.id.img_unison_item);
                viewHolder.mNameTx = (TextView)convertView.findViewById(R.id.tx_unision_item_name);
                viewHolder.mNumberTx = (TextView)convertView.findViewById(R.id.tx_unision_item_number);
                viewHolder.mSingTx = (TextView)convertView.findViewById(R.id.text_to_sing);
                viewHolder.mSingTx.setOnClickListener(this);
                viewHolder.mViewDummy = convertView.findViewById(R.id.view_dummy);
                viewHolder.mViewDummy.setOnClickListener(this);
                convertView.setTag(viewHolder);
                AutoUtils.autoSize(convertView);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }
            viewHolder.mViewDummy.setTag(position);
            viewHolder.mSingTx.setTag(position);

            return convertView;
        }

        @Override
        public void onClick(View view) {
            int position = (int)view.getTag();
            Toast.makeText(mContext, String.format("position: %s", position), Toast.LENGTH_SHORT).show();
            switch (view.getId())
            {
                case R.id.text_to_sing:
                    startActivity(new Intent(mContext, RecordActivity.class));
                    break;
                case R.id.view_dummy:
                    // TODO: 2017/6/10
                    break;
            }
        }
    }

    private class ViewHolder{
        ImageView mHeadImg;
        TextView mNameTx;
        TextView mNumberTx;
        TextView mSingTx;
        View mViewDummy;
    }
}
